package com.woniu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.pojo.Dept;
import com.woniu.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("dept")
public class DeptController {
    @Autowired
    DeptService deptService;
    //列表
    public String deptList(Model model){
        model.addAttribute("deptList",deptService.getDeptList());
        return "deptlist";
    }
    //增加
    @RequestMapping("deptSave")
    public String deptSave(Dept dept){
       deptService.saveDept(dept);
       return "redirect:deptList";
    }
    //删除
    @RequestMapping("deleteDept")
    public String deleteDept(Integer deptno){
        deptService.deleteDept(deptno);
        return "redirect:deptList";
    }
    //找到一个部门
    @RequestMapping("getOneDept")
    public String getOneDept(Integer deptno,Model model){
        model.addAttribute(deptService.getOneDept(deptno));
       return "deptUpdate";
    }
    //修改保存
    @RequestMapping("updateDept")
    public String updateDept(Dept dept){
        deptService.updateDept(dept);
        return "redirect:deptList";
    }
    //批量删除
    @RequestMapping("delDeptSelect")
    public String delDeptSelect(String[] deptnos){
        for (int i = 0; i <deptnos.length ; i++) {
            deptService.deleteDept(Integer.parseInt(deptnos[i]));
        }
        return "redirect:deptList";
    }
    //查询
    @RequestMapping(method = RequestMethod.GET)
    public String deptSelect(Dept dept, Model model, @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,@RequestParam(value = "pageSize",defaultValue = "6")Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
       List<Dept> deptList=deptService.getDeptSelect(dept);
        PageInfo<Dept> pageInfo=new PageInfo<Dept>(deptList);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("deptList",deptList);
        return "deptlist";
    }

}
